package com.server.server.message;

import lombok.Data;

import java.util.UUID;

/**
 * @Description: This is a sample class.
 * @Author: Simon
 */
@Data
public class MessageHeader {

    // 可以考虑优化协议 可能存在冗余

    /*
    +----------------------------------------------------------------------------+
    | 魔数 2byte | 协议版本号 1byte | 序列化算法 1byte | 报文类型 1byte | 加密算法 1byte
    +----------------------------------------------------------------------------+
    | 状态 1byte |               消息 ID 32byte                |      数据长度 4byte
    +----------------------------------------------------------------------------+
    */

    private short magic;

    private byte version;

    private byte serialization;

    private byte messageType;

    private byte messageStatus;

    private String requestId;

    private byte encryptedAlgorithm;

    private int dataLen;

    public MessageHeader setMagic(short magic) {
        this.magic = magic;
        return this;
    }

    public MessageHeader setVersion(byte version) {
        this.version = version;
        return this;
    }

    public MessageHeader setSerialization(byte serialization) {
        this.serialization = serialization;
        return this;
    }

    public MessageHeader setMessageType(byte messageType) {
        this.messageType = messageType;
        return this;
    }

    public MessageHeader setMessageStatus(byte messageStatus) {
        this.messageStatus = messageStatus;
        return this;
    }

    public MessageHeader setRequestId(String requestId) {
        this.requestId = requestId;
        return this;
    }

    public MessageHeader setEncryptedAlgorithm(byte encryptedAlgorithm) {
        this.encryptedAlgorithm = encryptedAlgorithm;
        return this;
    }

    public MessageHeader setDataLen(int dataLen) {
        this.dataLen = dataLen;
        return this;
    }

    public MessageHeader build() {
        if (requestId == null){
            // 32位
            requestId=UUID.randomUUID().toString().replaceAll("-","");
        }
        return this;
    }
}
